package com.wanlian.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import com.google.zxing.WriterException;
import com.wanlian.common.PictureTool;

public class PictureUtil
{

    private static Color contentcolor = new Color(255, 255, 255);

    private static Color titlecolor = new Color(108, 120, 144);

    private static Color ctcolor = new Color(162, 166, 169);

    private static Font ctfont = new Font("microsoft yahei", Font.BOLD, 14);

    private static Color endtimeLineColor = new Color(240, 240, 240);

    private static Color endBackLineColor = new Color(69, 92, 124);

    private static void createImage(String fileLocation, BufferedImage image)
    {
        try
        {
            FileOutputStream fos = new FileOutputStream(fileLocation);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            ImageIO.write(image, "JPEG", fos);
            bos.close();
            fos.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }



    private static void drawMain(int bwidth, int imageWidth, int imageHeight, int contentHight, Graphics graphics)
    {
        Color backcolor = new Color(240, 245, 248);
        graphics.setColor(backcolor);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        graphics.setColor(Color.black);

        graphics.setColor(contentcolor);
        graphics.fillRect(bwidth, bwidth, imageWidth - 2 * bwidth, contentHight);
        graphics.setColor(Color.black);
    }

    private static int calTotalheight(int hight)
    {
        // title
        hight += 50;
        hight += 5 * (30 + 8);
        hight += 40;
        hight += 30;
        hight += 50;
        return hight;
    }

    private static int drawTitle(Graphics graphics, double n, int width, int hight, String title)
    {
        int fontsize = (int) (20 * n);
        Font ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
        int twidth = getFontWidth(ctlfont, title);
        int lwidth = width / 2 - twidth / 2;
        graphics.setColor(titlecolor);
        graphics.setFont(ctlfont);
        graphics.drawString(title, lwidth, hight);
        hight += 50;
        return hight;
    }

    private static int drawDetail(Graphics graphics, double n, int bwidth, int width, int hight, int type)
    {
        if (type == 1)
        {
            for (int i = 0; i < 5; i++)
            {
                int fontsize = (int) (14 * n);
                Font ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
                String title = "股票分类";
                String value = "股票分类值";
                graphics.setColor(titlecolor);
                graphics.setFont(ctlfont);
                graphics.drawString(title, bwidth + 20, hight);

                graphics.setColor(ctcolor);
                int twidth = getFontWidth(ctlfont, value);
                int lwidth = width - twidth - 20;
                graphics.setFont(ctlfont);
                graphics.drawString(value, lwidth, hight);
                hight += (int) (10 * n);
                graphics.setColor(endtimeLineColor);
                graphics.drawLine(bwidth + 20, hight, width - 20, hight);
                hight += (int) (30 * n);
            }
        }
        else if (type == 0)
        {
            for (int a = 0; a < 2; a++)
            {
                int startwidth = bwidth + 20;
                int starthight = hight;
                for (int i = 0; i < 4; i++)
                {
                    int fontsize = (int) (18 * n);
                    Font ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
                    String title = "利率";
                    String value = "4.3%";

                    graphics.setColor(ctcolor);
                    int twidth = getFontWidth(ctlfont, value);
                    int lwidth = width - twidth - 20;
                    graphics.setFont(ctlfont);
                    graphics.drawString(value, startwidth, hight);

                    fontsize = (int) (15 * n);
                    ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
                    hight += 20 + fontsize;
                    graphics.setColor(titlecolor);
                    graphics.setFont(ctlfont);
                    graphics.drawString(title, startwidth, hight);
                    startwidth += (int) (width / 4);
                    hight = starthight;
                }

                graphics.setColor(endtimeLineColor);
                hight += (int) (50 * n);
                graphics.drawLine(bwidth + 20, hight, width - 20, hight);
                hight += 30;
            }
        }
        return hight;
    }

    private static int drawContact(Graphics graphics, double n, int bwidth, int width, int hight)
    {
        int fontsize = (int) (14 * n);
        Font ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
        String title = "联系方式";
        graphics.setColor(titlecolor);
        graphics.setFont(ctlfont);
        graphics.drawString(title, bwidth + 20, hight);

        fontsize = (int) (14 * n);
        ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
        int twidth = getFontWidth(ctfont, "手机");
        int lwidth = width / 2 - twidth / 2;
        graphics.setColor(ctcolor);
        graphics.setFont(ctlfont);
        graphics.drawString("手机 18675535030", lwidth, hight);

        fontsize = (int) (16 * n);
        ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
        twidth = getFontWidth(ctfont, "张仅限");
        graphics.setColor(ctcolor);
        graphics.setFont(ctlfont);
        graphics.drawString("张仅限", width - twidth, hight);
        hight += 40;
        return hight;
    }

    private static int drawInfo(Graphics graphics, double n, int bwidth, int width, int hight)
    {
        int fontsize = (int) (16 * n);
        Font ctlfont = new Font("microsoft yahei", Font.BOLD, fontsize);
        String value = "华夏银行";
        int twidth = getFontWidth(ctlfont, value);
        int lwidth = width - twidth - bwidth - 20;

        graphics.setColor(ctcolor);
        graphics.setFont(ctlfont);
        graphics.drawString(value, lwidth, hight);

        hight += 30;
        return hight;
    }

    private static int drawBanner(Graphics graphics, int bwidth, int width, int hight)
    {
        int rHeight = 60;
        graphics.setColor(endBackLineColor);
        graphics.fillRect(bwidth, hight, width, rHeight);
        graphics.setColor(endtimeLineColor);
        hight += 37;
        String title = "小融虾-区块链智能互联";
        graphics.setColor(contentcolor);
        Font bannerFont = new Font("microsoft yahei", Font.BOLD, 20);
        int twidth = getFontWidth(bannerFont, title);
        int lwidth = width / 2 - twidth / 2 + bwidth;
        graphics.setFont(bannerFont);
        graphics.drawString(title, lwidth, hight);
        hight += 37;
        return hight;

    }

    private static int drawProductInfo(Graphics graphics, int width, int hight)
    {
        String value = "长按扫描二维码 获取详细信息";
        String value1 = "更多标准化业务资讯";
        String value2 = "上小融虾APP唾手可得";

        Font productFont = new Font("microsoft yahei", Font.BOLD, 16);
        Color productColor = new Color(108, 120, 144);

        int twidth = getFontWidth(productFont, value);
        int lwidth = width - twidth - 50;
        graphics.setColor(productColor);
        graphics.setFont(productFont);
        graphics.drawString(value, lwidth, hight);

        hight += 30;
        int twidth1 = getFontWidth(productFont, value1);
        int lwidth1 = width - twidth1 - 50;
        graphics.setColor(productColor);
        graphics.setFont(productFont);
        graphics.drawString(value1, lwidth1, hight);

        hight += 30;
        int twidth2 = getFontWidth(productFont, value2);
        int lwidth2 = width - twidth2 - 50;
        graphics.setColor(productColor);
        graphics.setFont(productFont);
        graphics.drawString(value2, lwidth2, hight);

        return hight;
    }

    private static int getFontWidth(Font f, String value)
    {
        FontMetrics fm = sun.font.FontDesignMetrics.getMetrics(f);
        return fm.stringWidth(value);
    }

    public  void mergeImg(String path1, String path2, String path3, double n,HttpServletResponse response) throws IOException
    {
        InputStream imagein = new FileInputStream(path1);
        InputStream imagein2 = new FileInputStream(path2);

        BufferedImage image = ImageIO.read(imagein);
        BufferedImage image2 = ImageIO.read(imagein2);

        image2 = PictureTool.resize(image2, (int) (100 * n), (int) (100 * n));
        Graphics g = image.getGraphics();
        int x = image.getWidth()-438 ;
        int y = image.getHeight() -552;
        g.drawImage(image2, x, y, image2.getWidth() , image2.getHeight() , null);
       // OutputStream outImage = new FileOutputStream(path3);
       // ImageIO.write(image, "PNG", outImage);
        imagein.close();
        imagein2.close();
      //  outImage.close();

        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        ImageIO.write(image, "PNG", tmp);
        tmp.close();
        response.setHeader("content-length", String.valueOf(tmp.size()));
        response.getOutputStream().write(tmp.toByteArray());// 通过response输出图片

    }


    public static void mergeImg(String path1, String path2, String path3, double n) throws IOException
    {
        InputStream imagein = new FileInputStream(path1);
        InputStream imagein2 = new FileInputStream(path2);

        BufferedImage image = ImageIO.read(imagein);
        BufferedImage image2 = ImageIO.read(imagein2);
        image2 = PictureTool.resize(image2, (int) (100 * n), (int) (100 * n));
        Graphics g = image.getGraphics();
        int x = image.getWidth()-438 ;
        int y = image.getHeight() -552;
        g.drawImage(image2, x, y, image2.getWidth() + 10, image2.getHeight() + 5, null);
        OutputStream outImage = new FileOutputStream(path3);
        ImageIO.write(image, "jpg", outImage);
        imagein.close();
        imagein2.close();
        outImage.close();

    }


    public  void main(String[] args) throws FileNotFoundException, WriterException, IOException
    {
       /* double n = 1.2;
        int bwidth = (int) (30 * n);
        int imageWidth = (int) (500 * n);// 图片的宽度
        int imageHeight = 700;// 图片的高度
        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        int hight = 50;
        hight = calTotalheight(hight);
        drawMain(bwidth, imageWidth, imageHeight, hight, graphics);
        hight = bwidth + 50;
        hight = drawTitle(graphics, n, imageWidth, hight, "股票质押");
        hight = drawDetail(graphics, n, bwidth, imageWidth - bwidth, hight, 1);
        hight = drawContact(graphics, n, bwidth, imageWidth - 2 * bwidth, hight);
        hight = drawInfo(graphics, n, bwidth, imageWidth, hight);
        hight = drawBanner(graphics, bwidth, imageWidth - 2 * bwidth, hight);
        drawProductInfo(graphics, imageWidth - 180, hight + 50);
        createImage("D://test1.jpg", image);*/
        QrCodeCreateUtil.createQrCode(new FileOutputStream(new File("/Users/zhaoxiaotong/Downloads/a.png")), "1", 900, "PNG");
        ///Users/zhaoxiaotong/Downloads/a.png
       // mergeImg("/Users/zhaoxiaotong/Downloads/qr.png",  "/Users/zhaoxiaotong/Downloads/a.png","/Users/zhaoxiaotong/Downloads/m.png", 2.4, 100);
        System.out.println("完成");

    }

}